package site.fsyj.sign;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import site.fsyj.config.GenshinHelperProperties;
import site.fsyj.sign.genshin.GenShinSignMiHoYo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ponking
 * @Date 2021/5/31 15:54
 */
@Slf4j
public class DailyTask implements Runnable {


    public GenShinSignMiHoYo genShinSign;

    /**
     * @param account 账号配置信息
     */
    public DailyTask(GenshinHelperProperties.Account account) {
        genShinSign = new GenShinSignMiHoYo(account.getCookie());
    }

    @SneakyThrows
    @Override
    public void run() {
        doDailyTask();
    }

    public Map<String,Object> doDailyTask() {
        Map<String,Object> result = new HashMap<>();

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        StringBuilder stringBuilder = new StringBuilder();

        log.info("开始执行时间[ {} ],执行环境[ {} ]", dtf.format(LocalDateTime.now()), System.getProperty(Constant.GENSHIN_EXEC));

        stringBuilder.append("开始执行时间[ ").append(dtf.format(LocalDateTime.now())).append(" ],执行环境[ ").append(System.getProperty(Constant.GENSHIN_EXEC)).append(" ]");

        if (genShinSign != null) {
            List<Map<String, Object>> list = genShinSign.doSign();

            for (Map<String, Object> map : list) {
                if (!(boolean) map.get("flag")){
                    //登录失败，直接返回
                    map.put("msg",stringBuilder.toString() + "\n" + map.get("msg"));
                    return map;
                }

                stringBuilder.append("\n").append("-----------------\n").append(map.get("msg"));
            }
        }

        result.put("msg",stringBuilder.toString());
        result.put("flag",true);
        return result;
    }

}
